﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class FormDSSanPham : Window
    {
        public int masp;
        public FormDSSanPham()
        {
            InitializeComponent();
            hoiThoat = false;
        }

        private void FormDSSanPham_Load(object sender, EventArgs e)
        {
            List<SanPhamDTO> ds = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            ds = bus.LayDSSP();
            dataGridViewDSSP.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
            {
                dataGridViewDSSP.Rows.Add(ds[i].MaSP, ds[i].TenSP, ds[i].DonGiaKL,ds[i].DonGiaTV,ds[i].QuyCach,ds[i].DVT,ds[i].SLTon,ds[i].DiemCV);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            FormThemSanPham frm = new FormThemSanPham();
            frm.MdiParent = this.MdiParent;
            frm.Show();
            FormDSSanPham_Load(null, null);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            int row = dataGridViewDSSP.CurrentRow.Index;            
            masp = int.Parse(dataGridViewDSSP[0, row].Value.ToString());
            FormSuaSanPham frm = new FormSuaSanPham(masp);
            frm.MdiParent = this.MdiParent;
            frm.Show();
            FormDSSanPham_Load(null, null);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int row = dataGridViewDSSP.CurrentRow.Index;            
            masp = int.Parse(dataGridViewDSSP[0, row].Value.ToString());
            SanPhamBUS bus = new SanPhamBUS();
            if (MessageBox.Show(this, "Xóa sản phẩm có MãSP:  " + @masp + "?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int kq = bus.XoaSanPham(masp);
                if (kq != 0)
                {
                    MessageBox.Show(this, "Xóa thành công!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FormDSSanPham_Load(null, null);
                }
            }
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            List<SanPhamDTO> ds = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            if (this.txtTim.Text.Trim().Length!=0)
            {
                ds = bus.TimSanPham(txtTim.Text.Trim());                
                if (ds.Count>0)
                {
                    dataGridViewDSSP.Rows.Clear();
                    for (int i = 0; i <= ds.Count - 1; i++)
                    {
                        dataGridViewDSSP.Rows.Add(ds[i].MaSP, ds[i].TenSP, ds[i].DonGiaKL, ds[i].DonGiaTV, ds[i].QuyCach, ds[i].DVT, ds[i].SLTon, ds[i].DiemCV);
                    }
                } 
                else
                {
                    MessageBox.Show(this, "Không tìm thấy!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                
            }
            else
            {
                MessageBox.Show(this, "Nhập tên sản phẩm cần tìm!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }            
            
        }

        private void txtTim_TextChanged(object sender, EventArgs e)
        {
            List<SanPhamDTO> ds = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            if (this.txtTim.Text.Trim().Length != 0)
            {
                ds = bus.TimSanPham(txtTim.Text.Trim());
                if (ds.Count > 0)
                {
                    dataGridViewDSSP.Rows.Clear();
                    for (int i = 0; i <= ds.Count - 1; i++)
                    {
                        dataGridViewDSSP.Rows.Add(ds[i].MaSP, ds[i].TenSP, ds[i].DonGiaKL, ds[i].DonGiaTV, ds[i].QuyCach, ds[i].DVT, ds[i].SLTon, ds[i].DiemCV);
                    }
                }
                else
                {
                    //MessageBox.Show(this, "Không tìm thấy!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FormDSSanPham_Load(null, null);
                    return;
                }
            }
            else
            {
               // MessageBox.Show(this, "Nhập tên sản phẩm cần tìm!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }            
            
        }

        private void dataGridViewDSSP_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridViewDSSP.CurrentRow.Index;
            masp = int.Parse(dataGridViewDSSP[0, row].Value.ToString());
            FormSuaSanPham frm = new FormSuaSanPham(masp);
            frm.MdiParent = this.MdiParent;
            frm.Show();
            FormDSSanPham_Load(null, null);
        }

        private void dataGridViewDSSP_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        

           
    }
}
